<?php

namespace App\Admin\Controllers\cms;

use App\Models\Miao;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MiaoController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '撩小喵';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Miao);
        $grid->column('title','标题');
        $grid->column('status','状态')->using(['0'=>'不可用','1'=>'使用中']);
        $grid->column('created_at','创建时间');
        $grid->column('updated_at','修改时间');
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter) {
                $filter->like('title', '标题');
            });
            $filter->column(1/2, function ($filter) {
                $filter->between('created_at', '创建时间')->datetime();

            });

        });
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableColumnSelector();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Miao::findOrFail($id));
        $show->field('title','标题');
        $show->field('content','内容');
        $show->field('status','状态')->using(['0'=>'不可用','1'=>'使用中']);
        $show->field('created_at','创建时间');
        $show->field('updated_at','修改时间');


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Miao);

        $form->text('title', '标题')->rules('required');
        $form->textarea('content', '内容');
        $states = [
            'on'  => ['value' => 1, 'text' => '使用', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];

        $form->switch('status','状态')->states($states);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableReset();
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}
